<?php

//广告模块
class Ads_model extends Model {

	function Ads_model()
	{
		parent::Model();
		$this->load->database();
		$this->load->model("site_model", "sm");
	}
	
	//获取广告
	//超过一个广告则随机取
	function get($placeid, $siteid = 0)
	{
		$row = array();
		//检测广告位
		$place = $this->get_place($placeid);
		if(!$place["passed"]) return false;
		
//		$this->db->from("ads");
//		$this->db->where(array("placeid"=>$placeid, "passed"=>'1', "todate"=>'0'));
//		$this->db->or_where(array("todate <	"=>time()));
//		$query = $this->db->order_by("adsid", "random")->limit(1)->get();
		$time = time();
		if($place["formember"] == '1') {//会员广告
			$sql = "select * from rn_ads where placeid='$placeid' and passed='1' and siteid='$siteid' and (todate='0' or todate > $time) and fromdate <= $time order by adsid desc limit 1";
			$query = $this->db->query($sql);
	//		echo $this->db->last_query();
			if ($query->num_rows()>0) {
				$row = $query->row_array();
				//获取广告位信息
				$row["place"] = $this->get_place($placeid);
			}
		}elseif($place["forsite"] == '0') {//常规广告
			$sql = "select * from rn_ads where placeid='$placeid' and passed='1' and (todate='0' or todate > $time) and fromdate <= $time order by RAND() limit 1";
			$query = $this->db->query($sql);
	//		echo $this->db->last_query();
			if ($query->num_rows()>0) {
				$row = $query->row_array();
				//获取广告位信息
				$row["place"] = $this->get_place($placeid);
			}
		} else {//小站广告
			$sql = "select * from rn_ads where placeid='$placeid' and passed='1' and (todate='0' or todate > $time) and fromdate <= $time order by adsid desc";
			$query = $this->db->query($sql);
			if ($query->num_rows()>0) {
				$result = $query->result_array();
				foreach ($result as $r) {
					$adsid = $r['adsid'];
					//检测是否有应用小站
					$sites = $this->get_adsite($adsid);
					if(is_array($sites) && count($sites) == 0) {//应用所有
						$row = $this->show($adsid);
						//获取广告位信息
						$row["place"] = $this->get_place($placeid);
						return $row;
					}
					if(is_array($sites) && count($sites) > 0) {//应用某些小站
						foreach ($sites as $site) {
							$siteids[] = $site['id'];
						}
						if(in_array($siteid, $siteids)) {
							$row = $this->show($adsid);
							//获取广告位信息
							$row["place"] = $this->get_place($placeid);
							return $row;
						}
					}
				}
			}
		}
		return $row;
	}
	
	//获取广告位
	function get_place($placeid)
	{
		$q = $this->db->get_where("ads_place", array("placeid"=>$placeid));
		$place = $q->row_array();
		return $place;
	}
	
	//获取广告
	function show($adsid)
	{
		$q = $this->db->get_where("ads", array("adsid"=>$adsid));
		$row = $q->row_array();
		return $row;
	}
	
	//设置views
	function setviews($adsid)
	{
		$row = $this->show($adsid);
		$views = $row["views"];
		$data = array(
			"views" => $views+1,
		);
		$this->db->where("adsid", $adsid);
		$result = $this->db->update("ads", $data);
		return $result;
	}
	
	//设置clicks
	function setclicks($adsid)
	{
		$row = $this->show($adsid);
		$clicks = $row["clicks"];
		$data = array(
			"clicks" => $clicks+1,
		);
		$this->db->where("adsid", $adsid);
		$result = $this->db->update("ads", $data);
		return $result;
	}
	
	//获取所有广告位列表
	function list_allplaceitem($order = "placeid", $desc = "desc")
	{
		$items = array();
		$query = $this->db->from("ads_place")->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
		}
		return $items;
	}
	
	//获取列表
	function list_placeitem($offset = 0, $pagecount = 20, $order = "placeid", $desc = "desc")
	{
		$items = array();
		$query = $this->db->from("ads_place")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
		}
		return $items;
	}
	
	//计算总数
	function count_placeitems()
	{
		return $this->db->count_all_results("ads_place");
	}
	
	//获取列表
	function list_item($placeid = 0, $offset = 0, $pagecount = 20, $order = "adsid", $desc = "desc")
	{
		$items = array();
		$this->db->from("ads");
		if($placeid > 0) {
			$this->db->where("placeid", $placeid);
		} else {//屏蔽会员广告位
			$this->db->where_not_in("placeid", array(36,37,38));
		}
		$query = $this->db->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
			foreach ($items as $key => $item){
				$placeid = $item["placeid"];
				$place = $this->get_place($placeid);
				$items[$key]["place"] = $place;
			}
		}
		return $items;
	}
	
	//计算总数
	function count_items($placeid)
	{
		if ($placeid > 0) {
			$query = $this->db->get_where("ads", array("placeid"=>$placeid));
			return $query->num_rows();
		}
		return $this->db->count_all_results("ads");
	}
	
	//添加广告位
	function addplace($data)
	{
		//execute sql
		$result = $this->db->insert("ads_place", $data);
		return $result;
	}
	
	//编辑广告位
	function updateplace($placeid, $data)
	{
		//execute sql
		$this->db->where("placeid", $placeid);
		$result = $this->db->update("ads_place", $data);
		return $result;
	}
	
	//删除广告位
	function delselected_place($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//execute sql
		$this->db->where_in("placeid", $delid);
		$result = $this->db->delete("ads_place");
		//删除广告
		$this->db->where_in("placeid", $delid);
		$result = $this->db->delete("ads");
		return $result;
	}
	
	//屏蔽广告位
	function statuselected_place($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//execute sql
		$data = array(
			"passed" => 0,
		);
		$this->db->where_in("placeid", $delid);
		$result = $this->db->update("ads_place", $data);
		return $result;
	}
	
	//屏蔽广告
	function statuselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//execute sql
		$data = array(
			"passed" => 0,
		);
		$this->db->where_in("adsid", $delid);
		$result = $this->db->update("ads", $data);
		//屏蔽小站广告
		$this->db->where_in("adsid", $delid);
		$this->db->update("ads_site", $data);
		return $result;
	}
	
	//删除广告
	function delselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//删除广告
		$this->db->where_in("adsid", $delid);
		$result = $this->db->delete("ads");
		//删除小站广告
		$this->db->where_in("adsid", $delid);
		$this->db->delete("ads_site");
		return $result;
	}
	
	//删除广告
	function delete($adsid)
	{
		$this->db->where("adsid", $adsid);
		$result = $this->db->delete("ads");
		//删除小站广告
		$this->db->where("adsid", $adsid);
		$this->db->delete("ads_site");
		return $result;
	}
	
	//添加广告
	function add($data, $imageurl = 'imageurl')
	{
		//处理图片
		$imageurl = ciupload($imageurl);
		
		//等比裁剪
		if($imageurl == "") show_failed("图片上传失败");
		
		$place = $this->get_place($data['placeid']);
		$params = array('src_file' => './data/upload_pic/'.$imageurl, 'dst_file' => './data/upload_pic/'.$imageurl);
		//$this->load->library("imagecrop", $params);
		$this->load->library("imagecrop");
		$this->imagecrop->src_file=$params['src_file'];
		$this->imagecrop->dst_file=$params['dst_file'];
		$this->imagecrop->LoadImage();
		$this->imagecrop->Crop($place['width'],$place['height'],1);
		$this->imagecrop->SaveImage();
		$this->imagecrop->destory();
		
		$data["imageurl"] = $imageurl;
		//execute sql
		$result = $this->db->insert("ads", $data);
		$adsid = $this->db->insert_id();
		//处理应用小站
		if (p("sites") != "") {
			$sites = explode(",", p("sites"));
			foreach ($sites as $siteid) {
				if ($siteid != "") {
					$indata = array(
						"adsid" => $adsid,
						"siteid" => $siteid,
						"fromdate" => $data["fromdate"],
						"todate" => $data["todate"],
						"passed" => $data["passed"],
					);
					$this->db->insert("ads_site", $indata);
				}
			}
		}
		return $result;
	}
	
	//获取应用小站
	function get_adsite($adsid)
	{
		$sites = array();
		$query = $this->db->get_where("ads_site", array("adsid"=>$adsid));
		if ($query->num_rows()>0) {
			$result = $query->result_array();
			foreach ($result as $row){
				$siteid = $row["siteid"];
				$sites[] = $this->sm->get_site($siteid);
			}
		}
		return $sites;
	}
	
	//编辑广告
	function update($adsid, $data)
	{
		//处理图片
		$imageurl = ciupload("imageurl");
		if($imageurl && $imageurl != "") {
			$data["imageurl"] = $imageurl;
		}
		
		//execute sql
		$this->db->where("adsid", $adsid);
		$result = $this->db->update("ads", $data);
		
		//处理小站广告
		$updata = array(
			"fromdate" => $data["fromdate"],
			"todate" => $data["todate"],
			"passed" => $data["passed"],
		);
		$this->db->where("adsid", $adsid);
		$result = $this->db->update("ads_site", $updata);
		return $result;
	}
}

/* End of file */